//
//  CMISGetSites.m
//  Alfresco
//
//  Created by Michael Muller on 10/29/09.
//  Copyright 2009 Michael J Muller. All rights reserved.
//

#import "CMISGetSites.h"
#import "Site.h"

@implementation CMISGetSites

- (CMISGetSites *) initWithDelegate:(id <AsynchronousDownloadDelegate>) del {
	NSString *cql = @"select f.Name from folder as f where f.ObjectTypeId = 'F/st_site'";
	return (CMISGetSites *) [self initWithQuery:cql delegate:del];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
	
	// if this is a new entry, create a repository item and add it to the list
	if ([elementName isEqualToString:@"entry"]) {
		Site *s = [[Site alloc] init];
		[self.results addObject:s];
		[s release];
	}
	
	self.elementBeingParsed = elementName;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	Site *currentItem = [self.results lastObject];
	
	if ([self.elementBeingParsed isEqualToString:@"title"]) {
		currentItem.title = currentItem.title ? [currentItem.title stringByAppendingString:string] : string;
	}
	else if ([self.elementBeingParsed isEqualToString:@"content"]) {
		currentItem.node = currentItem.node ? [currentItem.node stringByAppendingString:string] : string;
	}
}

@end

/*

 sample response:
 
 <?xml version="1.0" encoding="UTF-8"?>
 <feed xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200901" xmlns:alf="http://www.alfresco.org" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
 <author><name>mmuller</name></author>
 <generator version="3.2.0 (2039)">Alfresco (Community)</generator>
 <icon>http://cm.ziaconsulting.com:80/alfresco/images/logo/AlfrescoLogo16.ico</icon>
 <id>urn:uuid:resultset</id>
 <link rel="self" href="http://cm.ziaconsulting.com:80/alfresco/service/api/query"/>
 <link rel="first" href="http://cm.ziaconsulting.com:80/alfresco/service/api/query?maxItems=3&amp;skipCount=0&amp;guest=" type="application/atom+xml;type=feed"/>
 <link rel="next" href="http://cm.ziaconsulting.com:80/alfresco/service/api/query?maxItems=3&amp;skipCount=3&amp;guest=" type="application/atom+xml;type=feed"/>
 <title>Result set for select f.Name from folder as f where f.ObjectTypeId = 'F/st_site'</title>
 <updated>2009-10-29T16:13:15.771Z</updated>
 <entry>
 <author><name>mmahon</name></author>
 <content>977a5305-6800-4825-ade3-73e53e032635</content>
 <id>urn:uuid:977a5305-6800-4825-ade3-73e53e032635</id>
 <link rel="self" href="http://cm.ziaconsulting.com:80/alfresco/service/api/node/workspace/SpacesStore/977a5305-6800-4825-ade3-73e53e032635"/>
 <link rel="edit" href="http://cm.ziaconsulting.com:80/alfresco/service/api/node/workspace/SpacesStore/977a5305-6800-4825-ade3-73e53e032635"/>
 <link rel="allowableactions" href="http://cm.ziaconsulting.com:80/alfresco/service/api/node/workspace/SpacesStore/977a5305-6800-4825-ade3-73e53e032635/permissions"/>
 <link rel="relationships" href="http://cm.ziaconsulting.com:80/alfresco/service/api/node/workspace/SpacesStore/977a5305-6800-4825-ade3-73e53e032635/rels"/>
 <link rel="parents" href="http://cm.ziaconsulting.com:80/alfresco/service/api/node/workspace/SpacesStore/977a5305-6800-4825-ade3-73e53e032635/parent"/>
 <link rel="children" href="http://cm.ziaconsulting.com:80/alfresco/service/api/node/workspace/SpacesStore/977a5305-6800-4825-ade3-73e53e032635/children"/>
 <link rel="descendants" href="http://cm.ziaconsulting.com:80/alfresco/service/api/node/workspace/SpacesStore/977a5305-6800-4825-ade3-73e53e032635/descendants"/>
 <link rel="type" href="http://cm.ziaconsulting.com:80/alfresco/service/api/type/F/st_site"/>
 <link rel="repository" href="http://cm.ziaconsulting.com:80/alfresco/service/api/repository"/>
 <title>newclienttemplate</title>
 <updated>2009-09-05T11:45:55.925Z</updated>
 <cmis:object>
 <cmis:properties>
 <cmis:propertyString cmis:name="BaseType"><cmis:value>folder</cmis:value></cmis:propertyString>
 <cmis:propertyString cmis:name="f.Name"><cmis:value>newclienttemplate</cmis:value></cmis:propertyString>
 </cmis:properties>
 </cmis:object>
 <cmis:terminator/>
 <alf:icon>http://cm.ziaconsulting.com:80/alfresco/images/icons/space-icon-default-16.gif</alf:icon>
 </entry>

*/